package com.up.vms.interfaces.rest.controller;

import com.up.vms.application.service.ShortVideoApplicationService;
import com.up.vms.domain.module.ShortVideoDTO;
import com.up.vms.domain.vm.entity.ShortVideo;
import com.up.vms.infrastructure.constants.EmpAPIConstants;
import com.up.vms.infrastructure.mvc.BaseResponse;
import com.up.vms.infrastructure.mvc.GeneRespDTO;
import com.up.vms.infrastructure.mvc.RespCode;
import com.up.vms.infrastructure.mvc.ReturnCode;
import com.up.vms.infrastructure.util.CommonUtils;
import com.up.vms.infrastructure.util.DateUtil;
import com.up.vms.interfaces.rest.request.ShortVideoRequest;
import com.up.vms.interfaces.rest.request.ShortVideoReviewRequest;
import com.up.vms.interfaces.rest.response.CommonResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@Api(value = "短视频管理接口", produces = "application/json")
@RequestMapping(EmpAPIConstants.API_ROOT)
@Slf4j
public class ShortVideoController {

    @Autowired
    private ShortVideoApplicationService shortVideoApplicationService;

    @RequestMapping(value = "/short/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取当前类型下的活动列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index", value = "页码", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页记录数", dataType = "Integer")
    })
    @ResponseBody
    public BaseResponse getCategoryList(
            @RequestParam(value = "categoryId", required = false) Integer categoryId,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "showVideoMode", required = false) Integer showVideoMode,
            @RequestParam(value = "start", required = false) String startTime,
            @RequestParam(value = "end", required = false) String endTime,
            @RequestParam(value = "shortName", required = false) String shortName,
            @RequestParam(value = "index") Integer index,
            @RequestParam(value = "size") Integer size) {
        GeneRespDTO<HashMap> geneRespDTO = null;
        CommonResponse campaignListResponse = new CommonResponse();
        ShortVideoDTO shortVideoDTO = new ShortVideoDTO();

        try {
            if (CommonUtils.isNotNull(index)) {
                shortVideoDTO.setPage(index);
            }
            if (CommonUtils.isNotNull(size)) {
                shortVideoDTO.setSize(size);
            }
            if (CommonUtils.isNotNull(status)) {
                shortVideoDTO.setStatus(status);
            }
            if (CommonUtils.isNotNull(categoryId)) {
                shortVideoDTO.setCategoryId(categoryId);
            }
            if (CommonUtils.isNotNull(showVideoMode)) {
                shortVideoDTO.setShowVideoMode(showVideoMode);
            }
            if (CommonUtils.isNotNull(startTime)) {
                shortVideoDTO.setStartTime(DateUtil.format(startTime, DateUtil.FORMAT_YMD));
            }
            if (CommonUtils.isNotNull(endTime)) {
                Date endDay = DateUtil.format(endTime, DateUtil.FORMAT_YMD);
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(endDay);
                calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
                endDay = calendar.getTime();
                shortVideoDTO.setEndTime(endDay);
            }

            geneRespDTO = shortVideoApplicationService.getShortVideoList(shortVideoDTO);

            campaignListResponse.setData(geneRespDTO.getData());
            campaignListResponse.setRespCode(new RespCode(ReturnCode.SUCCESS));

        } catch (Exception e) {
            log.error("获取短视频列表失败：{}", e.getMessage(), e.getCause());
            campaignListResponse.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR));
        }

        return campaignListResponse;
    }

    @RequestMapping(value = "/short/review", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse review(@RequestBody ShortVideoReviewRequest request) {
        CommonResponse commonResponse = new CommonResponse();

        try {
            List<String> shortIdList = new ArrayList<>();

            if (CommonUtils.isNull(request.getShortId())) {
                throw new Exception("shortId is null");
            }

            String shortId = request.getShortId();
            if (shortId.contains(",")) {
                String[] cs = shortId.split(",");
                for (String s : cs) {
                    shortIdList.add(s);
                }
            } else{
                shortIdList.add(shortId);
            }

            if (CommonUtils.isNull(request.getReviewResult())) {
                throw new Exception("result is null");
            }

            GeneRespDTO geneRespDTO = shortVideoApplicationService.review(shortIdList, request.getReviewResult());
            commonResponse.setRespCode(geneRespDTO.getRespCode());
            commonResponse.setData(geneRespDTO.getData());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            commonResponse.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR));
        }

        return commonResponse;
    }


    /**
     * 删除视频 token
     */
    @RequestMapping(value = "/short/delete", method = RequestMethod.POST)
    public BaseResponse delete(@RequestBody ShortVideoReviewRequest request) {
        CommonResponse response = new CommonResponse();
        try {
            List<String> shortIdList = new ArrayList<>();

            if (CommonUtils.isNull(request.getShortId())) {
                throw new Exception("shortId is null");
            }

            String shortId = request.getShortId();
            if (shortId.contains(",")) {
                String[] cs = shortId.split(",");
                for (String s : cs) {
                    shortIdList.add(s);
                }
            } else{
                shortIdList.add(shortId);
            }

            GeneRespDTO geneRespDTO = shortVideoApplicationService.delete(shortIdList);

            response.setRespCode(geneRespDTO.getRespCode());
            response.setData(geneRespDTO.getData());
        } catch (Exception ex) {
            log.error(ex.getMessage());
            response.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR));
        }

        return response;
    }

    @RequestMapping(value = "/short/save", method = RequestMethod.POST)
    public BaseResponse saveBanner(@RequestBody ShortVideoRequest request) {
        CommonResponse response = new CommonResponse();
        try {
            ShortVideo shortVideo = convertFromRequest(request);

            GeneRespDTO geneRespDTO = shortVideoApplicationService.saveShortVideo(shortVideo);

            response.setRespCode(geneRespDTO.getRespCode());
            response.setData(geneRespDTO.getData());
        } catch (Exception ex) {
            log.error(ex.getMessage());
            response.setRespCode(new RespCode(ReturnCode.SERVICE_ERROR));
        }

        return response;
    }

    private ShortVideo convertFromRequest(ShortVideoRequest request) {
        ShortVideo shortVideo = new ShortVideo();

        if (CommonUtils.isNotNull(request.getShortId())) {
            shortVideo.setShortId(request.getShortId());
        }

        shortVideo.setShortName(request.getShortName());
        shortVideo.setShortDesc(request.getShortDesc());
        shortVideo.setShortUrl(request.getShortUrl());
        shortVideo.setTelephone(request.getTelephone());
        shortVideo.setUploader(request.getUploader());

        shortVideo.setCategoryId(request.getCategoryId());
        shortVideo.setCategory(request.getCategory());
        shortVideo.setShowMode(request.getShowMode());
        shortVideo.setShowVideoMode(request.getShowVideoMode());
        shortVideo.setShowImage(request.getShowImage());
        shortVideo.setSorting(request.getSorting());
        shortVideo.setRecommendFlag(request.getRecommendFlag());

        return shortVideo;
    }
}
